Don't require current package in resolve_ws
authorAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 19 Aug 2016 18:00:21 +0000 (21:00 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 19 Aug 2016 18:10:00 +0000 (21:10 +0300)
src/cargo/core/workspace.rs
src/cargo/ops/resolve.rs
tests/install.rs

index d4c32e57247667248a383c0c5707cb377f1e8b2c..bdf00a44e8356580077d3f2abac8348abb050ce6 100644 (file)
@@ -6,7 +6,7 @@ use std::slice;
 use core::{Package, VirtualManifest, EitherManifest, SourceId};
 use core::{PackageIdSpec, Dependency};
 use ops;
-use util::{Config, CargoResult, Filesystem};
+use util::{Config, CargoResult, Filesystem, human};
 use util::paths;
 
 /// The core abstraction in Cargo for working with a workspace of crates.
@@ -143,13 +143,17 @@ impl<'cfg> Workspace<'cfg> {
     /// actually a "virtual Cargo.toml", in which case an error is returned
     /// indicating that something else should be passed.
     pub fn current(&self) -> CargoResult<&Package> {
+        self.current_opt().ok_or_else(||
+            human(format!("manifest path `{}` is a virtual manifest, but this \
+                           command requires running against an actual package in \
+                           this workspace", self.current_manifest.display()))
+        )
+    }
+
+    pub fn current_opt(&self) -> Option<&Package> {
         match *self.packages.get(&self.current_manifest) {
-            MaybePackage::Package(ref p) => Ok(p),
-            MaybePackage::Virtual(..) => {
-                bail!("manifest path `{}` is a virtual manifest, but this \
-                       command requires running against an actual package in \
-                       this workspace", self.current_manifest.display())
-            }
+            MaybePackage::Package(ref p) => Some(p),
+            MaybePackage::Virtual(..) => None
         }
     }
 
index 9ec3f55f4356624adae5047ecf15fbe08d5c13da..eb5713294845da5543cf8b187f453923467a4857 100644 (file)
@@ -17,7 +17,9 @@ pub fn resolve_ws(registry: &mut PackageRegistry, ws: &Workspace)
     let resolve = try!(resolve_with_previous(registry, ws,
                                              Method::Everything,
                                              prev.as_ref(), None));
-    if try!(ws.current()).package_id().source_id().is_path() {
+
+    // Avoid writing a lockfile if we are `cargo install`ing a non local package.
+    if ws.current_opt().map(|pkg| pkg.package_id().source_id().is_path()).unwrap_or(true) {
         try!(ops::write_pkg_lockfile(ws, &resolve));
     }
     Ok(resolve)
index af6efcfad7fd38f0e7b1219616b84c92c2c08266..45eabb782dfe9958d404bf4de5e6fae504702721 100644 (file)
@@ -555,7 +555,8 @@ fn git_repo() {
         .file("src/main.rs", "fn main() {}");
     p.build();
 
-    assert_that(cargo_process("install").arg("--git").arg(p.url().to_string()),
+    // use `--locked` to test that we don't even try to write a lockfile
+    assert_that(cargo_process("install").arg("--locked").arg("--git").arg(p.url().to_string()),
                 execs().with_status(0).with_stderr(&format!("\
 [UPDATING] git repository `[..]`
 [COMPILING] foo v0.1.0 ([..])